package com.example.c94s3plyhyfc.web;

import com.example.c94s3plyhyfc.bean.Result;
import com.example.c94s3plyhyfc.biz.BizException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Component
@ControllerAdvice  // AOP 实现
public class ExceptionIntercepter {

    @ExceptionHandler
    @ResponseBody
    /**
     * 异常拦截方法, 默认返回是 ModelAndView 类型, SpringMVC会将返回结果当成视图处理
     * Rsult不是视图, 时Json数据, 要添加 ResponseBody注解, 告知SpringMVC
     */
    public Result handler(BizException e) {
        return new Result(0, e.getMessage(), e.getErrors());
    }
}
